<template>
  <el-dialog
    v-model="visible"
    v-if="load"
    :title="form.id ? '编辑' : '新增'"
    width="600"
    align-center
    draggable
    @close="close"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :z-index="2000"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-align="right"
      label-width="100"
      label-position="top"
    >
      <el-row :gutter="20">
        <el-col :span="21">
          <el-form-item label="语言名称" prop="name">
            <el-input v-model.trim="form.name" placeholder="请输入" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="3" class="mt-7">
          <el-form-item label="" label-width="0" prop="status">
            <el-switch
              v-model="form.status"
              :active-value="1"
              :inactive-value="2"
              active-text="启用"
              inactive-text="禁用"
              inline-prompt
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="语言简称:" prop="short_name">
        <el-input v-model.trim="form.short_name" placeholder="请输入语言简称" clearable />
      </el-form-item>
      <el-form-item label="序号:" required>
        <el-input
          v-model.number="form.sort"
          min="1"
          oninput="value=value.replace(/[^\d]/g,'')"
          placeholder="请输入序号"
          clearable
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loading" @click="save(formRef)"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>
<script setup name="ComModal">
import api from "../api";
import { reactive, toRefs, getCurrentInstance } from "vue";
import { ElMessage } from "element-plus";
const { proxy } = getCurrentInstance();
const emits = defineEmits(["query"]);
const data = reactive({
  form: {},
  visible: false,
  loading: false,
  formRef: null,
  rules: {
    name: [{ required: true, message: "请输入语言名称" }],
    short_name: [{ required: true, message: "请输入语言简称" }],
  },
  load: false,
});
const { rules, form, visible, loading, formRef, load } = toRefs(data);
const open = async (id) => {
  if (!load.value) {
    load.value = true;
    await 1;
  }
  if (id) getDetail(id);
  visible.value = true;
};
const getDetail = (id) => {
  api.getDetail({ id }).then((res) => {
    form.value = res.data;
  });
};
const save = () => {
  if (!formRef.value) return;
  form.value.sort = form.value.sort || 0;
  formRef.value.validate((valid) => {
    if (valid) {
      loading.value = true;
      if (form.value.id) {
        api
          .update({
            ...form.value,
          })
          .then((res) => {
            ElMessage.success(res.message);
            emits("query");
            close();
          })
          .finally(() => {
            loading.value = false;
          });
      } else {
        api
          .add({
            ...form.value,
            sort: form.value.sort || 0,
          })
          .then((res) => {
            ElMessage.success(res.message);
            emits("query");
            close();
          })
          .finally(() => {
            loading.value = false;
          });
      }
    }
  });
};
const initForm = () => {
  form.value = getForm();
};
// 初始化
const getForm = () => {
  return {
    name: "",
    status: 1,
    short_name: "",
    sort: "",
  };
};

onMounted(() => {
  initForm();
});

const close = () => {
  initForm();
  visible.value = false;
};
defineExpose({ open });
</script>
